package myPicture;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class PictureAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	PictureDao dao = new PictureDao();
	private File[] image; // 文件本身
	private String[] imageFileName; // 上传的文件名
	String[] imageContentType; // 文件类型
	String idType;
	ArrayList<Picture> pictures = new ArrayList<Picture>();
	HttpServletResponse response = ServletActionContext.getResponse();
	private Picture picture;

	public ArrayList<Picture> getPictures() {
		return pictures;
	}

	public void setPictures(ArrayList<Picture> pictures) {
		this.pictures = pictures;
	}

	public String getIdType() {
		return idType;
	}

	public void setIdType(String idType) {
		this.idType = idType;
	}

	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public String[] getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String[] getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String[] imageContentType) {
		this.imageContentType = imageContentType;
	}

	public Picture getPicture() {
		return picture;
	}

	public void setPicture(Picture picture) {
		this.picture = picture;
	}

	// 添加照片
	public String add() {
		ServletContext app = ServletActionContext.getServletContext();
		String path = app.getRealPath("") + "/images";// 存放照片的路径
		for (int i = 0; i < image.length; i++) { // 循环找照片
			File myfile = new File(path, imageFileName[i]);// 把接收到的照片名给path
			if (!myfile.getParentFile().exists()) {// 如果没有存放的文件夹，
				myfile.getParentFile().mkdir();// 就新建一个
			}
			try {
				FileUtils.copyFile(image[i], myfile);// 拷贝文件（备份）
				pictures.get(i).setUrl("images/" + imageFileName[i]);// 得到并设置当前照片的路径
				dao.addPicture(pictures.get(i));// 将照片保存在数据库中
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "list";
	}

	// 得到照片的信息
	public String getpic() {
		int id = 0;
		if (idType.equals("user")) { // 判断得到的是照片还是用户的id
			id = picture.getUid();
		} else {
			id = picture.getId();
		}
		ArrayList<Picture> list = dao.getpicture(id, idType);// 将用户的信息保存在list中
		// 得到getContextPath的路径
		String path = ServletActionContext.getRequest().getContextPath();
		String json = PictureServler.getJSON(list, path);
		response.setCharacterEncoding("utf-8");
		try {
			PrintWriter out = response.getWriter();
			out.print(json); // 在页面上打印照片的信息
			System.out.println(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String getnum() {
		int num = dao.getPictureCount(picture.getUid());
		try {
			PrintWriter out = response.getWriter();
			out.print(num);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public String delete() {
		// 删除文件
		String url = dao.geturl(picture.getId());
		ServletContext tex = ServletActionContext.getServletContext();
		String path = tex.getRealPath("") + "/" + url;
		File myfile = new File(path); // 将路径包装成一个文件
		FileUtils.deleteQuietly(myfile);
		// 删除表中的记录
		dao.deletePicture(picture.getId());
		return null;
	}

}
